package com.fivefu.core.skyeye.event.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;

import com.fivefu.core.skyeye.event.services.DbAiSceneService;
import com.fivefu.core.skyeye.event.services.SceneService;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import com.fivefu.core.skyeye.event.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场景表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-15 13:45:56
 */
@Api(tags = "场景")
@RestController
@RequestMapping("/scene")
public class DbAiSceneController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(DbAiSceneController.class);

    @Resource
    private DbAiSceneService dbAiSceneService;
    @Autowired
    private SceneService sceneService;


    @RequestMapping(path = "/editScene",method = RequestMethod.POST)
    @ApiOperation(value = "编辑场景",httpMethod = "POST",response = ResultInfo.class,notes = "编辑场景")
    public ResultInfo editScene(@ModelAttribute @Valid SceneVo sceneVo, @RequestAttribute String userid){
        try {
            logger.info("编辑场景信息接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            String value = VerifyDataUtils.verifyRequiredColumn(sceneVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            //验证场景有效时间格式
            if (StrUtils.isNotNull(sceneVo.getSceneValidTime())){
                try {
                    Object timeArr[] = (Object[]) VerifyDataUtils.verifyJSONArray(sceneVo.getSceneValidTime());
                    for (int i = 0 ; i < timeArr.length ; i ++){
                        Object arr[] = (Object[]) timeArr[i];
                        for (int j = 0 ; j < arr.length ; j ++){
                            String time = (String) arr[j];
                            VerifyDataUtils.verifyDateTime(time, "HHmm");
                        }
                    }
                }catch (Exception e){
                    return ResultInfo.renderError("场景有效时间格式不正确");
                }
            }
            //验证场景特定识别参数格式
            if (StrUtils.isNotNull(sceneVo.getCvParam())){
                try {
                    VerifyDataUtils.verifyJSONArray(sceneVo.getCvParam());
                }catch (Exception e){
                    return ResultInfo.renderError("场景识别参数格式不正确");
                }
            }

            sceneService.editScene(sceneVo, userid);
            return ResultInfo.renderSuccess("编辑场景成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
      return ResultInfo.renderError("编辑场景失败");
    }

    @RequestMapping(path = "/enableScene",method = RequestMethod.POST)
    @ApiOperation(value = "开启、关闭场景",httpMethod = "POST",response = ResultInfo.class,notes = "开启、关闭场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景编号", paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "enable", value = "是否启用(0:否 1:是)", paramType = "query",required = true, dataType = "int"),
    })
    public ResultInfo enableScene(Long id,Integer enable, @RequestAttribute String userid){
        try {
            logger.info("开启、关闭场景接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景编号是否为空
            if (id == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            //判断是否启用是否为空
            if (enable == null){
                return ResultInfo.renderError("是否启用不能为空");
            }
            sceneService.enableScene(id,userid,enable);
            return ResultInfo.renderSuccess("开启、关闭场景成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("开启、关闭场景失败");
    }

    @RequestMapping(path = "/deleteScene",method = RequestMethod.POST)
    @ApiOperation(value = "删除场景",httpMethod = "POST",response = ResultInfo.class,notes = "删除场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景编号", paramType = "query",required = true, dataType = "Long"),
    })
    public ResultInfo deleteScene(Long id, @RequestAttribute String userid){
        try {
            logger.info("删除场景接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景编号是否为空
            if (id == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            sceneService.deleteScene(id,userid);
            return ResultInfo.renderSuccess("删除场景成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("删除场景失败");
    }

    @RequestMapping(path = "/querySceneDetail",method = RequestMethod.POST)
    @ApiOperation(value = "获取场景详情",httpMethod = "POST",response = ResultInfo.class,notes = "获取场景详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景编号", paramType = "query",required = true, dataType = "Long")
    })
    public ResultInfo querySceneDetail(Long id){
        try {
            logger.info("获取场景详情接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (id == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            Map<String, Object> scene = dbAiSceneService.querySceneDetail(id);
            return ResultInfo.renderSuccess(scene);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("获取场景详情失败");
    }

    @RequestMapping(path = "/querySceneByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页获取场景列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页获取场景列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", paramType = "query",required = true, dataType = "int")
    })
    public ResultInfo querySceneByPage(Integer page,Integer limit){
        try {
            logger.info("分页获取场景列表接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断当前页是否为空
            if (page == null){
                return ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit == null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            Page<Map<String, Object>> list = dbAiSceneService.querySceneByPage(page, limit);
            return ResultInfo.renderSuccess(list);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("分页获取场景列表失败");
    }

    @ApiOperation(value = "推送有切片的探头流地址",httpMethod = "GET",response = ResultInfo.class,notes = "推送有切片的探头流地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "protocal",value = "取流协议(共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "subType",value = "码流类型(0:主码流,1:辅流1,2:辅流2。默认为0主码流)只有大华有",paramType = "query",required = false,dataType = "int")
    })
    @RequestMapping(path = "/getSceneCameraList",method = RequestMethod.GET)
    public ResultInfo getSceneCameraList(String protocal,Integer subType) {
        try {
            List<UrlChannelVo> result = sceneService.getSceneCameraList(protocal,subType);
            return ResultInfo.renderSuccess(result);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("推送有切片的探头流地址失败");
    }
}
